<%@ page import="edu.zhku.web4.util.JdbcUtil" %>
<%@ page import="edu.zhku.web4.pojo.User" %>
<%@ page import="edu.zhku.web4.common.Constants" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>修改ing</title>
</head>
<body>
<%
    String userId = request.getParameter("id");
    User user = (User) session.getAttribute(Constants.USER_SESSION_ID);
    if (user != null && user.getId().equals(Long.valueOf(userId))) {
        Long id = Long.valueOf(userId);
        String username = request.getParameter("username");
        String password = request.getParameter("pwd1");
        Integer age = Integer.valueOf(request.getParameter("age"));
        int res = JdbcUtil.executeUpdate("UPDATE user SET username = ?, password = ?, age = ? WHERE id = ?", username, password, age, id);
        if (res > 0) {
            // 修改成功
            request.getSession().setAttribute(Constants.USER_SESSION_ID, new User(id, username, password, age));
            request.setAttribute("updateMsg", "修改成功");
            request.getRequestDispatcher("index.jsp").forward(request, response);
        } else {
            request.setAttribute("updateMsg", "修改失败，请重试");
            request.getRequestDispatcher("index.jsp").forward(request, response);
        }
    } else {
        // 未登录或尝试删除他人
        request.setAttribute("updateMsg", "只能修改自己");
        request.getRequestDispatcher("index.jsp").forward(request, response);
    }
%>
</body>
</html>
